
---------------------------------------------------------------------------------------
-- RTScriptPage Init Base Script
---------------------------------------------------------------------------------------

io.stdout:setvbuf("no")
io.stdin:setvbuf("no")

------------------------------------------------------------------------------------------------------------
-- Global Variable
------------------------------------------------------------------------------------------------------------

-- Show debug message or not
if (g_bDebugMsg==nil) then
	g_bDebugMsg = true
end

-- Show the API result in debug message
if (g_bDebugMsg_Result==nil ) then
	g_bDebugMsg_Result = true
end

-- Auto word wrap for printf()
if (g_bMsg_WordWrap==nil) then
	g_bMsg_WordWrap = false
end

-- Load wxWidget module
if (g_wxWidget_load==nil) then
	g_wxWidget_load = true
end

---------------------------------------------------------------------------------------
-- I/O function
---------------------------------------------------------------------------------------

function __printf_CStyle(fmt,...)
	io.write(string.format(fmt,...))
end

function __printf_LuaStyle(fmt,...)
	print( string.format(fmt,...) )
end

function __printf_Null(fmt,...)
	-- Noting to do
end

function changeMsgBehavior()

	if( g_bMsg_WordWrap )
	then
		printf = __printf_LuaStyle
	else
		printf = __printf_CStyle
	end

	if( g_bDebugMsg )
	then
		dbgprintf = __printf_CStyle
	else
		dbgprintf = __printf_Null
	end

	if( g_bDebugMsg_Result )
	then
		dbgprintresult = dbgprintf
	else
		dbgprintresult = __printf_Null
	end
end

function resultMsg( fmt, errCode )

	msg = string.format ( "\n%s Result : %s\n", fmt, RTBB_Result2String(errCode) )

	if( errCode ~= RT_BB_SUCCESS )
	then
		__printf_CStyle( msg );	-- Forcibly print error result.
	else
		dbgprintresult( msg );
	end

	return (errCode == RT_BB_SUCCESS)
end

changeMsgBehavior()


---------------------------------------------------------------------------------------
-- FILES I/O
---------------------------------------------------------------------------------------

function fopen( filename, mode )
	return io.open(filename,mode)
end

function fclose( fptr )
	return io.close(fptr)
end

function fprintf(fptr, strfmt, ...)
	fptr:write(strfmt:format(...))
end

function fwrite( data, size, count, fptr )

	-- We don't support it at this moment
	if size ~= 1 then
		return -1
	end

	local rawdata = ""
	for i=1,#data,1 do
		rawdata = string.format("%s%s", rawdata, string.char(data[i]))
	end

	return fptr:write(rawdata)
end

function fread ( data, size, count, fptr )

	-- We don't support it at this moment
	if size ~= 1 then
		return -1
	end

    local rawdata =  fptr:read("*all")

 	for i=1,count,1 do
		data[i] = string.byte(rawdata,i)
	end

	return 0
end

---------------------------------------------------------------------------------------
-- Bitwise function
---------------------------------------------------------------------------------------

bit = require("bit") -- init bitwise operation

--bit.tobit(x)          -- normalize number to the numeric range of
--                      -- bit operations (all bit ops use this implicitly)
--bit.tohex(x[,n])      -- convert x to hex with n digits (default 8)
--bit.bnot(x)           -- bitwise not of x
--bit.band(x1[,x2...])  -- bitwise and of x1, x2, ...
--bit.bor(x1[,x2...])   -- bitwise or of x1, x2, ...
--bit.bxor(x1[,x2...])  -- bitwise xor of x1, x2, ...
--bit.lshift(x, n)      -- left-shift of x by n bits
--bit.rshift(x, n)      -- logical right-shift of x by n bits
--bit.arshift(x, n)     -- arithmetic right-shift of x by n bits
--bit.rol(x, n)         -- left-rotate of x by n bits
--bit.ror(x, n)         -- right-rotate of x by n bits
--bit.bswap(x)          -- byte-swap of x (little-endian <-> big-endian)


---------------------------------------------------------------------------------------
-- Misc function
---------------------------------------------------------------------------------------

require "RTLuaMisc"

function mdelay( ms )
	dbgprintf( "Delay: %d ms\n", ms )
	LuaMisc_Sleep( ms );
end

function udelay( us )
	dbgprintf( "Delay: %.2f us\n", us )
	LuaMisc_Sleep( us );
end

function randomize()
	LuaMisc_Randomize();
end

function rand()
	return LuaMisc_Rand();
end

---------------------------------------------------------------------------------------
-- wxWidget (UI)
---------------------------------------------------------------------------------------

if ( g_wxWidget_load ) then
	require "wx"
	dofile("./wxLua/init.wlua")
end









